const DB = require('../db/index');
const enumData = require('../enumeration/index');
const exception = require('../exception');
const fw = require('../core/framework');

module.exports = {
    create(data, successFn, failFn) {
        this.findByCode(data, (dbRes) => {
            failFn(exception['1401'])
        }, (e) => {
            if (fw.isExit(e)) {
                failFn(exception.ex(e))
            } else {
                try {
                    DB.Device.create(data).then((res) => {
                        successFn({code: 0, data: res})
                    });
                } catch (e) {
                    failFn(exception.ex(e))
                }
            }
        });
    },
    update(data, successFn, failFn) {
        this.findById(data, (dbRes) => {
            try {
                DB.Device.update(data).then(() => {
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }
        }, (e) => {
            if (e) {
                failFn(exception.ex(e))
            } else {
                failFn(exception['1402'])
            }
        });
    },
    read(data,successFn, failFn) {
        try {
            DB.Device.read(data).then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    searchAllToAI(successFn, failFn) {
        try {
            DB.Device.searchAllToAI().then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    search(data, successFn, failFn) {
        try {
            DB.Device.searchAll(data).then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    searchAll(data, successFn, failFn) {
        try {
            DB.Device.search(data).then((res) => {
                successFn({
                    code: 0, data: res
                });
            });
        } catch (e) {
            failFn(exception.ex(e))
        }
    },
    delete(data, successFn, failFn) {
        this.findById(data, (dbRes) => {
            try {
                DB.Device.delete({id: data.id}).then(() => {
                    successFn({code: 0})
                });
            } catch (e) {
                failFn(exception.ex(e))
            }

        }, (e) => {
            e ? failFn(exception.ex(e)) : failFn(exception['1403'])
        });
    },
    findById(data, successFn, failFn) {
        try {
            DB.Device.search({id: data.id}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn(dbRes);
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    },
    findByCode(data, successFn, failFn) {
        try {
            DB.Device.search({merchant_id: data.merchant_id, code: data.code}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn(dbRes);
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    },
    getDataById(data, successFn, failFn) {
        let deviceId = typeof data == 'object' ? data.device_id || data.deviceId : data;
        console.log('device --- getDataById',deviceId);
        try {
            DB.Device.getDataById({id: deviceId}).then((dbResData) => {
                let dbRes = fw.getDBObject(dbResData);
                if (fw.isExit(dbRes)) {
                    if (fw.isFn(successFn)) successFn(dbRes);
                } else {
                    if (fw.isFn(failFn)) failFn();
                }
            })
        } catch (e) {
            if (fw.isFn(failFn)) failFn(e);
        }
    }

};
